﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
//using System.Windows.Forms;

namespace WiccanRede
{
    //[ComVisible(true)]
    public struct Data : IDataStorageStruct
    {
        string name;
        string modelPath;
        string modelFile;

        public Data(string name, string modelPath, string modelFilename)
        {
            this.name = name;
            this.modelPath = modelPath;
            this.modelFile = modelFilename;
        }

        public string GetName()
        {
            return this.name;
        }

        public string GetModelPath()
        {
            return this.modelPath;
        }

        public string GetModelFilename()
        {
            return this.modelFile;
        }

        public override string ToString()
        {
            return String.Format("Name: {0} Path: {1} Filename: {2}", this.name, this.modelPath, this.modelFile);
        }
    }


    [ComVisible(true)]
    public class DataStorage : IDataStorage
    {

        int currentItem = 0;

        List<Data> data;

        public DataStorage()
        {
            data = new List<Data>();
        }

        /// <summary>
        /// Metoda vytvori tabulku se jmenem a cestou k modelu
        /// </summary>
        /// <param name="path">Cesta ke skladu objektu</param>
        public void Load(String path)
        {
            String[] directories = null;
            try
            {
                directories = Directory.GetDirectories(path);
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                return;
            }

            foreach (String d in directories)
            {
                string directoryName = d.Remove(0, path.Length + 1);
                
                if (directoryName.StartsWith("."))
                    continue;

                string modelPath = String.Empty;

                string[] files = Directory.GetFiles(d);

                foreach (String f in files)
                {
                    if (f.EndsWith(".x") || f.EndsWith(".X"))
                    {
                        modelPath = f;
                        break;
                    }
                }

                if (modelPath.Length > 0)
                {
                    Data a = new Data(directoryName, Path.GetFullPath(d), Path.GetFullPath(modelPath));
                    data.Add(a);
                }
                
            }

        }

        public void ResetPosition()
        {
            currentItem = 0;
        }

        public bool MoveToNextItem()
        {
            if (currentItem < data.Count)
            {
                currentItem++;
                return true;
            }

            return false;
        }

        public IDataStorageStruct GetCurrentItem()
        {
            return data[currentItem];
        }

        public int GetItemCount()
        {
            return data.Count;
        }
    }
}
